package demo2;

class Animal1 {
    public String name;
    public int age;

    public Animal1() {
        System.out.println("Animal不带参数的构造方法");
    }
    //构造方法
    public Animal1(String name, int age) {
        this.name = name;
        this.age = age;
    }
/*    public Animal1 eat() {
        System.out.println(this.name+" 正在吃饭");
        return null;
    }*/
    public void eat() {
        System.out.println(this.name+" 正在吃饭");
    }
}
class Dog extends Animal1 {

    public Dog() {
        System.out.println("不带参数的构造方法Dog");
    }

    public Dog(String name, int age) {
        super(name, age); //只能放第一行，所以不能和this()同时在一个构造方法里
    }
    public void bark() {
        System.out.println(this.name+" 正在汪汪汪");
    }
    public void bark(int num) {
        for(int i = 0; i < num; i++) {
            System.out.println("汪");
        }
    }
/*    public Dog eat() {
        System.out.println(this.name+" 正在吃狗粮");
        return null;
    }*/
    @Override
    public void eat() {
        System.out.println(this.name+" 正在吃饭");
    }
}

public class Test3 {

    public static void main(String[] args) {
        Animal1 animal1 = new Dog("小狗",12);
        Dog dog = (Dog)animal1;
        dog.bark();

        if(animal1 instanceof Cat) {
            Cat cat = (Cat) animal1;
            cat.mimi();
        }
    }

    public static void testFunc(Animal1 animal1) {
        animal1.eat();
    }
    //什么是多态？同一个引用调用同一个方法，当这个引用所引用的对象不一样时，那么所呈现的行为也不一样
    public static void main4(String[] args) {
        Dog dog = new Dog("小狗",12);
        testFunc(dog);
        System.out.println("+++++++++");
        Cat cat = new Cat("小猫",23);
        testFunc(cat);
    }

    public static Animal1 testFunc2() {
        return new Dog();
    }

    public static Dog testFunc3() {
        return new Dog();
    }
    public static void main3(String[] args) {
        Animal1 animal1 = testFunc3();

        Dog dog = new Dog("小狗",12);
        testFunc(dog);
    }
    public static void main2(String[] args) {
/*        Dog dog = new Dog("小狗",12);
        //向上转型
        Animal1 animal1 = dog;*/
        Animal1 animal1 = new Dog("小狗",12);
        //animal1.bark();//此时animal只能调用父类自己的方法
        //运行时绑/动态绑定
        animal1.eat();
    }
    public static void main1(String[] args) {
        Dog dog = new Dog("gougou",23);
        dog.eat();
    }
}


class Cat extends Animal1 {

    public Cat(String name, int age) {
        super(name, age); //只能放第一行，所以不能和this()同时在一个构造方法里
    }
    public Cat() {
        System.out.println("不带参数的构造方法Cat");
    }

    public void mimi() {
        System.out.println(this.name+" 正在喵喵喵");
    }
    public void eat() {
        System.out.println(this.name+" 正在吃猫粮");
    }
}